package com.darthsith.droidathome.core.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.darthsith.droidathome.core.dao.ArduinoBoardDao;
import com.darthsith.droidathome.core.model.ArduinoBoard;

@Repository
public class ArduinoBoardDaoImpl implements ArduinoBoardDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addArduinoBoard(ArduinoBoard arduinoBoard) {
		sessionFactory.getCurrentSession().save(arduinoBoard);
	}

	@Override
	public ArduinoBoard getArduinoBoard(String ipAddress) {
		return (ArduinoBoard) sessionFactory.getCurrentSession()
			.createQuery("FROM ARDUINOBOARD WHERE IPADDRESS=?")
				.setParameter(0, ipAddress).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ArduinoBoard> listAll() {
		return sessionFactory.getCurrentSession().createQuery("FROM ARDUINOBOARD").list();
	}

	@Override
	public void removeArduinoBoard(ArduinoBoard arduinoBoard) {
		String ipAddress=arduinoBoard.getIpAddress();
		ArduinoBoard a=getArduinoBoard(ipAddress);
		if (a!=null)
			sessionFactory.getCurrentSession().delete(a);
	}

}
